package com.wnhoo.smartfishtank.contants.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wnhoo.smartfishtank.R;
import com.wnhoo.smartfishtank.api.data.DataPlug;
import com.wnhoo.smartfishtank.fishtank.DataPlug_ListActivity;
import com.wnhoo.smartfishtank.ui.BadgeView;
import com.wnhoo.smartfishtank.ui.SlideView;

import java.util.ArrayList;
import java.util.List;


public class AdapterDataPlug extends BaseAdapter implements SlideView.OnSlideListener {
    // 填充数据的list
    private List<DataPlug> list;
    // 上下文
    private Context context;
    // 用来导入布局
    private LayoutInflater inflater = null;
    private SlideView mLastSlideViewWithStatusOn;

    // 构造器
    public AdapterDataPlug(List<DataPlug> list, Context context) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    public void setmList(ArrayList<DataPlug> mList) {
        this.list = mList;
        this.notifyDataSetChanged();

    }

    public void setTypeface(Typeface typeface) {
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        SlideView slideView = (SlideView) convertView;
            View itemView = inflater.inflate(R.layout.dataplug_list_item, null);

            slideView = new SlideView(context);
            slideView.setContentView(itemView);

            holder = new ViewHolder(slideView);
            slideView.setOnSlideListener(this);
            slideView.setTag(holder);

        holder.dataplug_item_name.setText(list.get(position).getTitle());
//        holder.dataplug_item_head.setImageResource(R.drawable.mic_device_icon);
        holder.dataplug_rel_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemTimingListener.ItemOnclick(position, v, list.get(position).getCategory());
            }
        });
        holder.deleteHolder.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
//                mMessageItems.remove(position);
//                adapter.notifyDataSetChanged();
                Toast.makeText(context, "删除第" + position + "个条目", 0).show();
            }
        });
        if (list.get(position).getRemind() == 1) {
            //当前有提醒，显示气泡
            holder.badge4.show();
        } else {
            holder.badge4.hide();
        }
        return slideView;
    }

    @Override
    public void onSlide(View view, int status) {
        if (mLastSlideViewWithStatusOn != null && mLastSlideViewWithStatusOn != view) {
            mLastSlideViewWithStatusOn.shrink();
        }
        if (status == SLIDE_STATUS_ON) {
            mLastSlideViewWithStatusOn = (SlideView) view;
        }
    }

    final class ViewHolder {
        TextView dataplug_item_name;
        ImageView dataplug_item_head, dataplug_item_icon;
        RelativeLayout dataplug_rel_item;
        BadgeView badge4;
        ViewGroup deleteHolder;

        ViewHolder(View view) {
            deleteHolder = (ViewGroup) view.findViewById(R.id.holder);
            dataplug_item_name = (TextView) view.findViewById(R.id.dataplug_item_name);
            dataplug_rel_item = (RelativeLayout) view.findViewById(R.id.dataplug_rel_item);
            badge4 = new BadgeView(context, dataplug_rel_item);
            badge4.setText("1");
            badge4.setBadgeBackgroundColor(Color.parseColor("#ff0000"));
            dataplug_item_head = (ImageView) view.findViewById(R.id.dataplug_item_head);
            dataplug_item_icon = (ImageView) view.findViewById(R.id.dataplug_item_icon);
        }
    }

    private OnItemTimingListener onItemTimingListener;

    public void setOnItemTimingListener(OnItemTimingListener l) {
        onItemTimingListener = l;
    }

    public interface OnItemTimingListener {
        boolean ItemOnclick(int position, View view, int category);
    }

}
